﻿
@{
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>table模块快速使用</title>

    <script src="~/scripts/jquery-3.5.1.js"></script>

    <link href="~/lib/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
</head>
<body>
    <form class="layui-form" onsubmit="return false" @*id="frmSel"*@>
        <div class="layui-form-item">
            <label class="layui-form-label">商品名称</label>
            <div class="layui-input-inline">
                <input type="text" name="shopName" id="ShopName" lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
            </div>
            <label class="layui-form-label">商品类别</label>
            <div class="layui-input-inline">
                <input type="text" name="shopTypeName" id="shopTypeId" lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-input-inline">
                <button class="layui-btn" id="Check">查询</button>
                <button class="layui-btn" id="Add">添加</button>
            </div>
        </div>
    </form>

    <script src="~/lib/layui-v2.5.6/layui/layui.js"></script>
    <table id="demo" lay-filter="test"></table>
    <script type="text/html" id="barDemo">
        @*<a class="layui-btn layui-btn-xs " lay-event="rec">推荐</a>*@
        <a class="layui-btn layui-btn-xs" lay-event="detail">查看</a>
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        <a class="layui-btn layui-btn-xs" lay-event="photo">图片</a>
    </script>
    <script type="text/html" id="Swich">
        <input type="checkbox" name="Status" value="{{d.Id}}" lay-filter="test" lay-skin="switch" lay-text="上架|下架" {{ d.Status == "1" ? "checked" : "" }}>
    </script>
    <script type="text/html" id="Image">
        @*<img src="http://localhost:9528/FrontDesk/img/{{d.Shopimage}}" style="width:60px;height:40px;"/>*@

        <img src="{{d.Shopimage}}"  style="width:60px;height:40px;"/>
    </script>
    <script>
        layui.use(['layer', 'table', 'form'], function () {
            var table = layui.table;
            var $ = layui.$;
            var form = layui.form;
            var layer = layui.layer;
            //第一个实例
            table.render({
                elem: '#demo'
                , height: 462
                , id: 'idTest'
                , url: '/Home/Shop' //数据接口
                , page: true //开启分页
                , cols: [[ //表头
                    { field: 'Id', title: 'ID', width: 80, sort: true, fixed: 'left' }
                    //,{ field: 'ShopId', title: '商品编号', width: 100 }
                    , { field: 'ShopName', title: '名字', width: 90, sort: true }
                    , { field: 'Shopimage', width: 90, title: '上下架', templet: "#Image" }
                    //, { field: 'ProductProfile', title: '简介', width: 80 }
                    , { field: 'ShopTypeName', title: '商品类别', width: 100 }
                    , { field: 'RecommendId', title: '推荐位置', width: 100, sort: true }
                    //, { field: 'Display', title: '职业', width: 80 }
                    , { field: 'Price', title: '价格', width: 100, sort: true }
                    , { field: 'Number', title: '库存数量', width: 100, sort: true }
                    , { field: 'GuideId', title: '规格', width: 80, sort: true }
                    , { field: 'Status', width: 90, title: '上下架', templet: "#Swich" }
                    , { fixed: 'right', width: 230, align: 'center', toolbar: '#barDemo' } //这里的toolbar值是模板元素的选择器
                ]]
            });
            form.on('switch(test)', function (res) {
                var con = this.value;
                console.log(123);
                $.ajax({
                    url: "/Home/ShopStatus?id=" + con,
                    type: "post",
                    data: '',
                    success: function (res) {
                        layer.msg(res.msg);
                    }
                });
            });
            $("#Add").click(function () {
                console.log(12321);
                layer.open({
                    title: '添加',
                    type: 2
                    , content: '/Home/AddView'
                    , area: ['500px', '600px']
                });
                return false;
            });
            table.on('tool(test)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
                var data = obj.data; //获得当前行数据
                var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）

                if (layEvent === 'detail') { //查看
                    layer.open({
                        title: '查看',
                        type: 2
                        , content: '/Shop/CheckView?id=' + data.Id
                        , area: ['500px', '600px']
                    });
                } else if (layEvent === 'del') { //删除
                    layer.confirm('真的删除行么', function (index) {
                        obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                        $.ajax({
                            url: "/Home/Del?id=" + data.Id,
                            type: "Post",
                            data: data,
                            success: function (re) {
                                layer.msg(re);
                                layer.close(index);
                            }
                        });
                    });
                } else if (layEvent === 'edit') { //编辑
                    layer.open({
                        title: '编辑',
                        type: 2
                        , content: '/Home/EditView?id=' + data.Id
                        , area: ['500px', '600px']
                    });
                }
                else if (layEvent === 'photo') { //图片
                    //do something
                    layer.open({
                        type: 2,
                        area: ['900px', '550px'],
                        content: '/Shop/photos?Id=' + data.Id
                    });
                };
                $("#Check").click(function () {
                    table.reload('idTest', {
                        url: '/Home/Shop'
                        , where: {
                            shopName: $("#ShopName").val(),
                            shopTypeName: $("#shopTypeId").val()
                        } //设定异步数据接口的额外参数
                        //,height: 300
                    });
                    return false;
                });

            })
        });
    </script>

</body>
</html>